﻿using System;
using System.Net;
using SCG.SolarVengeanceEngine.Orders;
using System.Collections.Generic;
using SCG.General;

namespace SCG.SolarVengeanceEngine.StarShips
{
    //RepairShips
    public class RepairShip : StarShip
    {
        //constructor
        public RepairShip(SVGame game, Player owner, int engines, StarSystem origin)
            : base(game, owner, engines, 0, origin)
        {
        }

        //Name
        public override string Name
        {
            get
            {
                return "RepairShip";
            }
        }

        //description
        public override string Description
        {
            get
            {
                return "RepairShips repair damaged StarShips adjacent to them.  They can also be directed to repair a specific StarShip.  RepairShips will also slowly repair themselves if damaged and there are no other damaged StarShips next to them to repair.";
            }
        }

        //ship type
        public override StarShipType ShipType
        {
            get
            {
                return StarShipType.RepairShip;
            }
        }

        //they can target any starship for repair
        public override bool CanExecuteOrder(OrderType ot)
        {
            switch (ot)
            {
                case OrderType.TargetStarShip:
                    return true;
                default:
                    return base.CanExecuteOrder(ot);
            }
        }
        public override bool CanTargetStarShip(StarShip ship, bool respectAlliance)
        {
            return true;
        }

        //Repair any adjacent friendly starship on PostMove, with preference to targeted ship
        protected internal override void PreMoveProcessing()
        {
            //see if the repairship is adjacent to its target           
            if (Order != null && Order.OrderType == OrderType.TargetStarShip)
            {
                MapObjectOrder moo = Order as MapObjectOrder;
                if (IsAdjacent(moo.StarShip) && moo.StarShip.IsDamaged)
                {
                    Game.RepairStarShip(moo.StarShip);
                    return;
                }
            }

            //see if there are eligable starships next to it to repair
            List<SCGPoint> pts = Game.AdjacentPoints(Location, false);
            foreach (SCGPoint pt in pts)
            {
                SVMapObject svm = Game.GetObjectAt((int)pt.X, (int)pt.Y);
                if (svm != null && svm is StarShip)
                {
                    StarShip target = svm as StarShip;
                    if (target.Owner == Owner && target.IsDamaged)
                    {
                        Game.RepairStarShip(target);
                        return;
                    }
                }
            }

            //Lastly it can repair itself 25% of the time
            if (IsDamaged)
                if (SVGame.RNG.NextDouble() > 0.75)
                    Game.RepairStarShip(this);
        }
    }
}
